//
//  BugPigBox.m
//  PigMarket
//
//  Created by 张敬 on 2019/9/9.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "BugPigBox.h"
#import "BugPigCell.h"
#import "SelectTribes.h"


//展示Viwe高度
#define kCategoryHeight 421

@interface BugPigBox ()<UITableViewDelegate, UITableViewDataSource>

/**
 MeatShopCartBox 's TableView -> UITableView
 */
@property (nonatomic, strong) UITableView *tableView;


/**
 MeatShopCartBox 's Header -> UIView
 */
@property (nonatomic, strong) UIView *header;

@property (nonatomic, strong) UILabel *detailLab;

@property (nonatomic, assign) float totalHeight;

@property (nonatomic, strong) BugPigCell *aimsCell;
@property (nonatomic, strong) DiscountCoupons *aimsItem;
@property (nonatomic, assign) NSInteger aimIndex;

@end


@implementation BugPigBox

- (void)setupUI{
    
    [super setupUI];
    
    _aimsItem = nil;
    _aimIndex = -1;
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    self.contentView.backgroundColor = KClearColor;
    [self.contentView addSubview:self.tableView];
    
}

- (void)setupContentLayout{
    
    self.height = KScreenHeight;
    
    CGFloat height = self.totalHeight;
    
    if (height > kCategoryHeight) {
        height = kCategoryHeight;
    }
    
    {
        self.contentView.mj_x = 0;
        self.contentView.mj_y = KScreenHeight  - height;
        self.contentView.width = KScreenWidth;
        self.contentView.height = height;
    }
    
    {
        self.tableView.frame = CGRectMake(0, 0, KScreenWidth, height);
    }
    
}


#pragma mark - tableview delegate/dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BugPigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BugPigCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row < self.datas.count) {
         DiscountCoupons *item = self.datas[indexPath.row];
        cell.item = item;
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < self.datas.count) {
        DiscountCoupons *item = self.datas[indexPath.row];
//
//        if (self.aimsItem) {
//            self.aimsItem.isSelect = NO;
//        }
        
        item.isSelect = !item.isSelect;
        self.aimsItem = item.isSelect ?item :nil;
        [self.tableView reloadData];
    }
    
}


#pragma mark - setter

- (void)setDatas:(NSArray *)datas{
    
    _datas = datas;
    
    self.totalHeight = 120*datas.count +52+ TJ_StatusBarHeight_Define;
    [self setupContentLayout];
}


#pragma mark - getter

- (UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 120;
        _tableView.backgroundColor = KClearColor;
        _tableView.tableHeaderView = self.header;
        [_tableView registerClass:[BugPigCell class] forCellReuseIdentifier:@"BugPigCell"];
        
        UIView *footer = [UIView new];
        footer.frame = CGRectMake(0, 0, KScreenWidth, TJ_StatusBarHeight_Define);
        footer.backgroundColor = KWhiteColor;
        _tableView.tableFooterView = footer;
        
    }
    return _tableView;
}


- (UIView *)header{
    if (!_header) {
        _header = [[UIView alloc] init];
        _header.backgroundColor = KWhiteColor;
        _header.frame = CGRectMake(0, 0, KScreenWidth, 52);
        [self drawCornerByView:_header];
        
        
        UILabel *titleLab = [UILabel makeLabelWithText:@"请选择豚券" textColor:UIColorFromRGB(0x24253D) font:mFont(16) textAliment:0];
        titleLab.frame = CGRectMake(15, 0, KScreenWidth/2.0, 52);
        [_header addSubview:titleLab];
        
        _detailLab = [UILabel makeLabelWithText:@"确定" textColor:mMainColor font:mFont(16) textAliment:2];
        _detailLab.frame = CGRectMake(0, 0, KScreenWidth/2.0, 52);
        _detailLab.mj_x = KScreenWidth - _detailLab.width - 15;
        
        CAShapeLayer *line = [CAShapeLayer layer];
        line.fillColor = mBackgroudColor.CGColor;
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 51, KScreenWidth, 1)];
        line.path = path.CGPath;
        [_header.layer addSublayer:line];
        
        
        @weakify(self);
        _detailLab.tapBlock = ^{
            @strongify(self);
            
            if (self.selectBlock) {
                self.selectBlock(self.aimsItem);
            }
            
            [kNotificationCenter postNotificationName:KMeatMarketCartBoxHiddenNotification object:nil];
        };
        
        [_header addSubview:_detailLab];
        
    }
    return _header;
}


- (void)drawCornerByView:(UIView *)view{
    
    CGRect rect = CGRectMake(0, 0, view.width, 52);
    UIRectCorner corners = UIRectCornerTopLeft|UIRectCornerTopRight;
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    _header.layer.mask = shape;
}


@end
